9 typedef pair
<int, int> node
;
10 typedef map
<node
, set
<node
> > graph
;
21 void dfs(const node
&u
){
22 set
<node
> &vecinos
= g
[u
];
23 for (set
<node
>::iterator i
= vecinos
.begin(); i
!= vecinos
.end(); ++i
){
25 if (d
[*i
] + 1 < d
[u
]){
26 //printf("Cycle from (%d, %d) to (%d, %d)\n", i->first, i->second, u.first, u.second);
28 longest
= max(longest
, d
[u
] - d
[*i
] + 1);
37 void connect(const node
&u
, const node
&v
){
45 while (cin
>> w
>> h
&& w
&& h
){
52 for (int i
=0; i
<w
; ++i
){
53 for (int j
=0; j
<h
; ++j
){
58 for (int i
=0; i
<w
; ++i
){
59 for (int j
=0; j
<h
; ++j
){
62 connect(node(i
, 2*j
), node(i
, 2*j
-1));
63 connect(node(i
, 2*j
+1), node(i
, 2*j
+2));
73 if (0 <= ni
&& ni
< w
&& 0 <= nj
&& nj
< h
&&
74 m
[ni
][nj
] != m
[i
][j
]){
76 connect(node(i
, 2*j
+1), node(ni
, 2*nj
+1));
82 if (0 <= ni
&& ni
< w
&& 0 <= nj
&& nj
< h
&&
83 m
[ni
][nj
] != m
[i
][j
]){
85 connect(node(i
, 2*j
), node(ni
, 2*nj
));
94 if (0 <= ni
&& ni
< w
&& 0 <= nj
&& nj
< h
&&
95 m
[ni
][nj
] != m
[i
][j
]){
96 connect(node(i
, 2*j
), node(ni
, 2*nj
));
102 if (0 <= ni
&& ni
< w
&& 0 <= nj
&& nj
< h
&&
103 m
[ni
][nj
] != m
[i
][j
]){
104 connect(node(i
, 2*j
+1), node(ni
, 2*nj
+1));
109 cerr
<< "Unrecognized char in input" << endl
;
114 /*for (map<node, set<node> >::iterator i = g.begin(); i != g.end(); ++i){
115 printf("Vecinos de (%d, %d):\n", i->first.first, i->first.second);
116 set<node> v = i->second;
117 for (set<node>::iterator j = v.begin(); j != v.end(); ++j){
118 printf("(%d, %d) ", j->first, j->second);
123 for (map
<node
, set
<node
> >::iterator i
= g
.begin(); i
!= g
.end(); ++i
){
124 if (d
.count(i
->first
) == 0){
130 printf("Maze #%d:\n", mazeNo
++);
132 printf("There are no cycles.\n");
134 printf("%d Cycles; the longest has length %d\n", qty
, longest
);